package com.ztgf.order.service.purchase;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.db.utils.RedisUtil;
import com.ztgf.dubbo.exception.ServiceException;
import com.ztgf.order.models.bo.purchase.PurchaseCommodityLeaveMessageBO;
import com.ztgf.order.models.dto.purchase.PurchaseCommodityLeaveMessageReplyDTO;
import com.ztgf.order.models.service.purchase.IPurchaseCommodityLeaveMessageReplyService;
import com.ztgf.order.models.vo.purchase.PurchaseCommodityLeaveMessageAndReplyVO;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.sys.models.dto.SysUserDTO;
import com.ztgf.user.models.dto.UserInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.order.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.order.mapper.purchase.PurchaseCommodityLeaveMessageMapper;
import com.ztgf.order.models.service.purchase.IPurchaseCommodityLeaveMessageService;
import com.ztgf.order.models.bo.purchase.PurchaseCommodityLeaveMessageSaveBO;
import com.ztgf.order.models.dto.purchase.PurchaseCommodityLeaveMessageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 询价留言表控制器
 *
 * @author mxli
 * @Date 2021-08-24 15:42:41
 */
@DubboService
@Slf4j
public class PurchaseCommodityLeaveMessageServiceImpl extends ServiceImpl<PurchaseCommodityLeaveMessageMapper, PurchaseCommodityLeaveMessageDTO> implements IPurchaseCommodityLeaveMessageService{

    @DubboReference
    private IShopInfoService shopInfoService;
    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private YmlConfig ymlConfig;
    @DubboReference
    private IPurchaseCommodityLeaveMessageReplyService purchaseCommodityLeaveMessageReplyService;

    @Override
    @Transactional(rollbackFor= ServiceException.class)
    public boolean add(PurchaseCommodityLeaveMessageSaveBO saveBO) {
        PurchaseCommodityLeaveMessageDTO dto = new PurchaseCommodityLeaveMessageDTO();
        if (null !=saveBO) {
            ShopInfoDTO shopInfoDTO = shopInfoService.selectById(saveBO.getShopId());
            if (shopInfoDTO!=null) {
                BeanUtils.copyProperties(saveBO, dto);
                dto.setShopName(shopInfoDTO.getShopName());
                dto.setStatus(0);
            }
            if(saveBO.getCreateUserId() == null){
                Long userId = null;
                String userName = "";
                Object object = redisUtil.getSession(CommonParameter.getHeadParamsBO ().getSessionId (), CommonParameter.getHeadParamsBO ().getSource ().toString ());
                if(object == null){
                    throw new ServiceException(CodeEnum.FAIL_100002);
                }
                if(object instanceof UserInfoDTO){
                    userId = ((UserInfoDTO) object).getId ();
                    userName = ((UserInfoDTO) object).getName ();
                } if(object instanceof SysUserDTO){
                    userId = ((SysUserDTO) object).getId ();
                    userName = ((SysUserDTO) object).getName ();
                }
                dto.setCreateUserId(userId);
                dto.setCreateUserName(userName);
            }
            if (saveBO.getPublishUserType()==0){
                dto.setShopName(dto.getCreateUserName());
            }
        }
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public Page<List<PurchaseCommodityLeaveMessageAndReplyVO>> queryByPageList(PurchaseCommodityLeaveMessageBO bo) {
        //查询出留言表的分页数据
        Wrapper<PurchaseCommodityLeaveMessageDTO> wrapper = new EntityWrapper<>();
        List<String> objects = new ArrayList<>();
        objects.add("create_time");
        wrapper.orderDesc(objects);
        wrapper.eq("purchase_id",bo.getPurchaseId());
        if (bo.getCommodityId()!=null){
            wrapper.eq("commodity_id",bo.getCommodityId());
        }
        Page<PurchaseCommodityLeaveMessageDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = this.selectPage(page, wrapper);
        Page<List<PurchaseCommodityLeaveMessageAndReplyVO>> page1= new Page<>(bo.getPageNo(), bo.getPageSize());
        page1.setTotal(page.getTotal());
        if (null==page.getRecords()){
            return page1;
        }
        //根据留言id查询留言的回复内容
        List<PurchaseCommodityLeaveMessageAndReplyVO> purchaseCommodityLeaveMessageDTOList=new ArrayList<>();
        for (PurchaseCommodityLeaveMessageDTO purchaseCommodityLeaveMessageDTO : page.getRecords()) {
            if(purchaseCommodityLeaveMessageDTO.getPublishUserType()!=2) {
                if (purchaseCommodityLeaveMessageDTO.getShopId().equals(bo.getShopId())) {
                    purchaseCommodityLeaveMessageDTO.setShopName(purchaseCommodityLeaveMessageDTO.getShopName());

                } else {
                    purchaseCommodityLeaveMessageDTO.setShopName(purchaseCommodityLeaveMessageDTO.getShopName().substring(0, 1) + "******");
                }
            }else {
                purchaseCommodityLeaveMessageDTO.setShopName(purchaseCommodityLeaveMessageDTO.getShopName());
            }
            PurchaseCommodityLeaveMessageAndReplyVO purchaseCommodityLeaveMessageAndReplyVO = new PurchaseCommodityLeaveMessageAndReplyVO();
            if (purchaseCommodityLeaveMessageDTO.getStatus()==1) {
                purchaseCommodityLeaveMessageDTO.setComments(null);
                purchaseCommodityLeaveMessageDTO.setUrl(null);
                purchaseCommodityLeaveMessageDTO.setPdfUrl(null);
            }
            purchaseCommodityLeaveMessageAndReplyVO.setPurchaseCommodityLeaveMessageDTO(purchaseCommodityLeaveMessageDTO);
            EntityWrapper<PurchaseCommodityLeaveMessageReplyDTO> wrapper1 = new EntityWrapper<>();
            wrapper1.eq("parent_id", purchaseCommodityLeaveMessageDTO.getId());
            wrapper1.orderAsc(objects);
            List<PurchaseCommodityLeaveMessageReplyDTO> list = purchaseCommodityLeaveMessageReplyService.selectList(wrapper1);
            if (list != null && list.size() > 0) {
                for (PurchaseCommodityLeaveMessageReplyDTO purchaseCommodityLeaveMessageReplyDTO : list) {
                    if (purchaseCommodityLeaveMessageReplyDTO.getReplyedPublishUserType()==2) {
                        purchaseCommodityLeaveMessageReplyDTO.setReplyedShopName(purchaseCommodityLeaveMessageReplyDTO.getReplyedShopName());
                    }else {
                        if (purchaseCommodityLeaveMessageReplyDTO.getReplyedShopId().equals(bo.getShopId())) {
                            purchaseCommodityLeaveMessageReplyDTO.setReplyedShopName(purchaseCommodityLeaveMessageReplyDTO.getReplyedShopName());
                        } else {
                            purchaseCommodityLeaveMessageReplyDTO.setReplyedShopName(purchaseCommodityLeaveMessageReplyDTO.getReplyedShopName().substring(0, 1) + "******");
                        }
                    }
                    if (purchaseCommodityLeaveMessageReplyDTO.getPublishUserType()==2) {
                        purchaseCommodityLeaveMessageReplyDTO.setShopName(purchaseCommodityLeaveMessageReplyDTO.getShopName());
                    }else {
                        if (purchaseCommodityLeaveMessageReplyDTO.getShopId().equals(bo.getShopId())) {
                            purchaseCommodityLeaveMessageReplyDTO.setShopName(purchaseCommodityLeaveMessageReplyDTO.getShopName());
                        } else {
                            purchaseCommodityLeaveMessageReplyDTO.setShopName(purchaseCommodityLeaveMessageReplyDTO.getShopName().substring(0, 1) + "******");
                        }
                    }
                    if (purchaseCommodityLeaveMessageReplyDTO.getStatus()==1){
                        purchaseCommodityLeaveMessageReplyDTO.setComments(null);
                        purchaseCommodityLeaveMessageReplyDTO.setUrl(null);
                        purchaseCommodityLeaveMessageReplyDTO.setPdfUrl(null);
                    }
                }
                purchaseCommodityLeaveMessageAndReplyVO.setList(list);
            }
            purchaseCommodityLeaveMessageDTOList.add(purchaseCommodityLeaveMessageAndReplyVO);
        }
        page1.setRecords(Collections.singletonList(purchaseCommodityLeaveMessageDTOList));
        return page1;
    }

    @Override
    public Page<List<PurchaseCommodityLeaveMessageAndReplyVO>> queryByApiPageList(PurchaseCommodityLeaveMessageBO bo) {
        //查询出留言表的分页数据
        Wrapper<PurchaseCommodityLeaveMessageDTO> wrapper = new EntityWrapper<>();
        List<String> objects = new ArrayList<>();
        objects.add("create_time");
        wrapper.orderDesc(objects);
        wrapper.eq("purchase_id",bo.getPurchaseId());
        if (bo.getCommodityId()!=null){
            wrapper.eq("commodity_id",bo.getCommodityId());
        }
        Page<PurchaseCommodityLeaveMessageDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = this.selectPage(page, wrapper);
        Page<List<PurchaseCommodityLeaveMessageAndReplyVO>> page1= new Page<>(bo.getPageNo(), bo.getPageSize());
        page1.setTotal(page.getTotal());
        if (null==page.getRecords()){
            return page1;
        }
        //根据留言id查询留言的回复内容
        List<PurchaseCommodityLeaveMessageAndReplyVO> purchaseCommodityLeaveMessageDTOList=new ArrayList<>();
        for (PurchaseCommodityLeaveMessageDTO purchaseCommodityLeaveMessageDTO : page.getRecords()) {
            PurchaseCommodityLeaveMessageAndReplyVO purchaseCommodityLeaveMessageAndReplyVO = new PurchaseCommodityLeaveMessageAndReplyVO();
            purchaseCommodityLeaveMessageAndReplyVO.setPurchaseCommodityLeaveMessageDTO(purchaseCommodityLeaveMessageDTO);
            EntityWrapper<PurchaseCommodityLeaveMessageReplyDTO> wrapper1 = new EntityWrapper<>();
            wrapper1.eq("parent_id", purchaseCommodityLeaveMessageDTO.getId());
            wrapper1.orderAsc(objects);
            List<PurchaseCommodityLeaveMessageReplyDTO> list = purchaseCommodityLeaveMessageReplyService.selectList(wrapper1);
            if (list != null && list.size() > 0) {
                purchaseCommodityLeaveMessageAndReplyVO.setList(list);
            }
            purchaseCommodityLeaveMessageDTOList.add(purchaseCommodityLeaveMessageAndReplyVO);
        }
        page1.setRecords(Collections.singletonList(purchaseCommodityLeaveMessageDTOList));
        return page1;
    }

    @Override
    @Transactional(rollbackFor=ServiceException.class)
    public boolean addByApi(PurchaseCommodityLeaveMessageSaveBO saveBO) {
        PurchaseCommodityLeaveMessageDTO dto = new PurchaseCommodityLeaveMessageDTO();
        if (null !=saveBO) {
                BeanUtils.copyProperties(saveBO, dto);
                dto.setStatus(0);
            if(saveBO.getCreateUserId() == null){
                Long userId = null;
                String userName = "";
                Object object = redisUtil.getSession(CommonParameter.getHeadParamsBO ().getSessionId (), CommonParameter.getHeadParamsBO ().getSource ().toString ());
                if(object == null){
                    throw new ServiceException(CodeEnum.FAIL_100002);
                }
                if(object instanceof UserInfoDTO){
                    userId = ((UserInfoDTO) object).getId ();
                    userName = ((UserInfoDTO) object).getName ();
                } if(object instanceof SysUserDTO){
                    userId = ((SysUserDTO) object).getId ();
                    userName = ((SysUserDTO) object).getName ();
                }
                dto.setCreateUserId(userId);
                dto.setCreateUserName(userName);
            }
                dto.setShopId(dto.getCreateUserId());
                dto.setShopName(dto.getCreateUserName());
        }
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }
}
